package br.com.lh.urabuilder.view.arrow
{
	import br.com.lh.urabuilder.util.CoordinatesUtils;
	
	import flash.display.CapsStyle;
	import flash.display.JointStyle;
	import flash.display.LineScaleMode;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point; // for PI

    public class UraArrow extends Sprite {
    	
    	private var arrowLength:uint = 10;
        private var arrowAngle:uint  = 30;
        private var trapezoid:Shape;
		private var startPoint:Point;
		private var endPoint:Point;
        
    	private const SIZE_ARROW_X:int = 5;
		private const SIZE_ARROW_Y:int = 10;
    	    
	    public function getStartPoint():Point{
	    	return startPoint;
	    }
	   
	    public function getEndPoint():Point{
	    	return endPoint;
	    }
   
		private function drawBranch():void{
			//trapezoid.graphics.clear();
			trapezoid.graphics.beginFill(0x000000);	
			trapezoid.graphics.moveTo(endPoint.x, endPoint.y);
			drawBranchLine(true);
	        trapezoid.graphics.moveTo(endPoint.x, endPoint.y);
	        drawBranchLine(false);
            
            drawBranchLine(true);
            trapezoid.graphics.endFill();

		}
        private function drawBranchLine(side:Boolean):void{
            var angleSide:Number = side ==true ? 1 : -1;
            var rad:Number = arrowAngle * angleSide * Math.PI / 180;
            var m:Matrix = new Matrix();
            var arrowPointU:Point = new Point();   //wanted
            var vector:Point = new Point(-(endPoint.x - startPoint.x), -(endPoint.y - startPoint.y));
            m.rotate(rad);  // angle has to be Rad
            vector = m.transformPoint(vector);
            vector.normalize(arrowLength);
            arrowPointU.x = endPoint.x + vector.x;
            arrowPointU.y = endPoint.y + vector.y;
            trapezoid.graphics.lineTo(arrowPointU.x, arrowPointU.y);  //draw the "branch"
    	}
		
		//ToDo[DSD] - Remove the constants and make it dynamic       
       private function drawBranchSelf():void{
//       		var angleSide:Number = -1;
//            var rad:Number = Math.PI/6;
//            var m:Matrix = new Matrix();
//            var arrowPointU:Point = new Point();   //wanted
//            var vector:Point = new Point(startPoint.x,startPoint.y);
//            m.rotate(rad);  // angle has to be Rad
//            vector = m.transformPoint(vector);
//            //vector.normalize(arrowLength);
//            arrowPointU.x = startPoint.x + vector.x;
//            arrowPointU.y = startPoint.y + vector.y;
			trapezoid.graphics.beginFill(0xFF0000);
	   	    trapezoid.graphics.moveTo((CoordinatesUtils.SELF_ARROW_WIDTH >> 1) + startPoint.x,CoordinatesUtils.SELF_ARROW_HEIGHT);
            trapezoid.graphics.lineTo((CoordinatesUtils.SELF_ARROW_WIDTH >> 1) - SIZE_ARROW_X + startPoint.x,CoordinatesUtils.SELF_ARROW_HEIGHT - SIZE_ARROW_Y);
       		//trapezoid.graphics.moveTo((CoordinatesUtils.SELF_ARROW_WIDTH >> 1),CoordinatesUtils.SELF_ARROW_HEIGHT);
            trapezoid.graphics.lineTo((CoordinatesUtils.SELF_ARROW_WIDTH >> 1) - (SIZE_ARROW_X << 1) + startPoint.x,CoordinatesUtils.SELF_ARROW_HEIGHT - (SIZE_ARROW_Y >> 2));  
			trapezoid.graphics.moveTo((CoordinatesUtils.SELF_ARROW_WIDTH >> 1) - (SIZE_ARROW_X << 1) + startPoint.x,CoordinatesUtils.SELF_ARROW_HEIGHT - (SIZE_ARROW_Y >> 2));  
			trapezoid.graphics.lineTo((CoordinatesUtils.SELF_ARROW_WIDTH >> 1) - SIZE_ARROW_X + startPoint.x,CoordinatesUtils.SELF_ARROW_HEIGHT - SIZE_ARROW_Y);
			trapezoid.graphics.endFill();
       }
       
        public function UraArrow(startPoint:Point,endPoint:Point,self:Boolean = false){
            this.startPoint = startPoint;    
            this.endPoint = endPoint;    
            trapezoid = new Shape();
            trapezoid.graphics.lineStyle(1, 0x000000, 1, false,
                                                  LineScaleMode.VERTICAL,
                                                  CapsStyle.NONE,
                                                  JointStyle.MITER, 10);
           if (self){
	           trapezoid.graphics.lineStyle(3,0);
	           trapezoid.graphics.moveTo(startPoint.x, startPoint.y);
	           trapezoid.graphics.drawEllipse(startPoint.x,startPoint.y,CoordinatesUtils.SELF_ARROW_WIDTH,CoordinatesUtils.SELF_ARROW_HEIGHT);
			   drawBranchSelf();
	           this.addChild(trapezoid);		
           }
           else{
			   this.drawBranch();	           
	    	   trapezoid.graphics.lineStyle(2,0x0);
	    	   trapezoid.graphics.moveTo(startPoint.x, startPoint.y);
	           trapezoid.graphics.lineTo(endPoint.x, endPoint.y);  //draw the "trunk" 
	           this.addChild(trapezoid);
           }

        }
    }

    
} 
